/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.util;

import static com.dataoke.shoppingguide.app773026.bean.Constants.BROWSER_BASE_URL;
import static com.dataoke.shoppingguide.app773026.bean.Constants.BROWSER_ID_URL;
import static com.dataoke.shoppingguide.app773026.bean.Constants.GET_MSG_URL;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuGetMsgBean;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuGetMsgResBean;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuIndexBaseBean;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class HaoDanKuUtil {
    private static Toast toast;
    private static List<HaodankuGetMsgBean> msgs;

    private static int index = 0;

    private static ImageView mHeadImg;
    private static TextView mTitle;
    private static View view;


    private static void initMsg() {
        HttpTools.get(GET_MSG_URL, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e("HaoDanKuUtil", e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                try {
                    HaodankuGetMsgResBean resBean = JSON.parseObject(response.body().string(), HaodankuGetMsgResBean.class);
                    if(resBean.getCode() == 200) {
                        msgs = resBean.getData().getMsgs();

                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    public static void toastMessage(Activity fragment) {
        if(msgs == null) {
            initMsg();
            initView(fragment);
            return;
        }
        if(toast != null) {
            toast.cancel();
        }

        HaodankuGetMsgBean msgBean = msgs.get(index);
        if(index >= (msgs.size()-1)) {
            index = 0;
        } else {
            index++;
        }

        GlideUtils.loadAvHeadImg(fragment, msgBean.getImg(), mHeadImg);
        mTitle.setText(msgBean.getMsg());

        toast = new Toast(fragment);
        toast.setGravity(Gravity.START|Gravity.TOP, 2, 200);//setGravity用来设置Toast显示的位置，相当于xml中的android:gravity或android:layout_gravity
        toast.setDuration(Toast.LENGTH_SHORT);//setDuration方法：设置持续时间，以毫秒为单位。该方法是设置补间动画时间长度的主要方法
        toast.setView(view); //添加视图文件
        toast.show();
    }

    protected static void initView(Fragment fragment) {
        LayoutInflater inflater = fragment.getLayoutInflater();//调用Activity的getLayoutInflater()
        view = inflater.inflate(R.layout.view_scroll, null); //加載layout下的布局
        mHeadImg = view.findViewById(R.id.iv_av);
        mTitle = view.findViewById(R.id.tv_msg);

    }

    protected static void initView(Activity fragment) {
        LayoutInflater inflater = fragment.getLayoutInflater();//调用Activity的getLayoutInflater()
        view = inflater.inflate(R.layout.view_scroll, null); //加載layout下的布局
        mHeadImg = view.findViewById(R.id.iv_av);
        mTitle = view.findViewById(R.id.tv_msg);

    }

    public static String getUrl(HaodankuIndexBaseBean bean) {
        String link = bean.getLink();
        if(link.startsWith("http")) {
            return link;
        }else if(link.startsWith("#")) {
            return BROWSER_BASE_URL+link;
        } else if(bean.getLink_type() == 3){
            String tag;
            switch (link){
                case "9" :
                    tag = "hot_sale";
                    break;
                case "35" :
                    tag = "gather_new";
                    break;
                case "7" :
                    tag = "juhuasuan";
                    break;
                case "31" :
                    tag = "signin";
                    break;
                case "18" :
                    tag = "new_maochao";
                    break;
                case "6" :
                    tag = "lowprice";
                    break;
                case "34" :
                    tag = "rt_xb";
                    break;
                case "12" :
                    tag = "fangyi";
                    break;
                case "51" :
                    tag = "activity125";
                    break;
                case "38" :
                    tag = "tb_subsidies";
                    break;
                case "73" :
                    tag = "super_selected";
                    break;
                case "64" :
                    tag = "tb_minishop";
                    break;
                case "76" :
                    tag = "activity_fresh";
                    break;
                case "66" :
                    tag = "tb_live";
                    break;
                default:
                    tag = "hot_sale";
                    break;
            }
            return BROWSER_BASE_URL + "&tmp=" + tag + "&code=sQwOpnhd&sp=#/sp";
        } else if(bean.getLink_type() == 4) {
            return BROWSER_BASE_URL + "#/custom-activity?activity=" + link;
        }
        return BROWSER_ID_URL + link;
    }

    public static String getCorner(String title) {
        switch (title){
            case "高德打车":
                return "超抵";
            case "支付宝红包":
                return "HOT";
            case "抖音电商":
                return "直播间";
            case "低价包邮":
                return "火爆";
            case "外卖红包":
                return "大红包";
//            case "商超红包":
//                return "HOT";
            case "群壁纸":
                return "免费壁纸";
//            case "饿了么":
//                return "红包";
            default:
                return "";

        }
    }
}
